﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DingTida.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace DingTida.Controllers
{
    public class MainController : Controller
    {
        public EasyStarContext db;
        public MainController(EasyStarContext _db)
        {
            this.db = _db;
        }
        public IActionResult Index()
        {

            return View();
        }

        public IActionResult other()
        {

            return View();
        }
        [HttpGet]
        public IActionResult logoff()
        {
            HttpContext.Session.SetString("username","");
            return Redirect("/home/index");



        }

        [HttpPost]
        public IActionResult changepwd(string pwd)
        {
            var user= HttpContext.Session.GetString("username");
            var realuser = db.EmEmpData.Where(c => c.EmpNo == user).FirstOrDefault();
            realuser.Pwd = pwd;
            db.SaveChanges();

            return Ok(new { msg="密码修改成功"});
        
        
        }

        [HttpGet]
        public IActionResult changepwd()
        {

            return View();
        
        }



        [HttpPost]
        public IActionResult login(string username, string password)
        {
            if (username == string.Empty || password == string.Empty || username == "" || password == "")
            {

                return Ok(new { msg = "帐户密码不能为空" });

            }
            else
            {

                if (db.EmEmpData.Any(c => c.EmpNo == username && c.Pwd == password))
                {
                    HttpContext.Session.SetString("username",username);
                    return Ok(new { msg = "ok" });
                }
                else
                {
                    return Ok(new { msg = "帐户或密码错误" });

                }

            }


        }

        [HttpGet]
        public IActionResult main()
        {

            var user = HttpContext.Session.GetString("username");
            if (user != null && user != string.Empty && user != "")
            {
                return View();
            }
            else {

                return View("other");
            
            }

           
        }

        [HttpGet]
        public IActionResult GetDeptList( int page, int limit)
        {
            var empid = HttpContext.Session.GetString("username");
            var depts = db.EsDept.Where(c => c.ExtPhone == empid && c.IfStop != true).ToList();
            return Ok(   new { 
              code=0,
              msg="",
              count=depts.Count,
              data=depts.OrderBy(c => c.Id).Skip((page - 1) * (limit)).Take(limit).ToList(),
            
            });

        
        

            
        }

        public IActionResult GetDeptUserList(string deptid, int page, int limit)
        {

            if (deptid == null || deptid == "" || deptid == string.Empty)
            {

                return Ok(new { 
                
                    code =0,
                    msg="请选择左边的部门，来获取员工列表",
                    count=0,
                    data= "" 
                });
            }
            else {
                var lst = db.EmEmpData.Where(c => c.Dept == deptid && c.LzDate == null).Select(c => new { c.Fname, c.EmpNo, c.EBm }).ToList();

                return Ok(new
                {
                    code = 0,
                    msg = "数据返回成功",
                    count = lst.Count,
                    data = lst.OrderBy(c => c.EmpNo).Skip((page - 1) * (limit)).Take(limit).ToList(),


                });
            }


        }

        [HttpGet]
        public IActionResult BanchAprrove()
        {
            return View();
        }
     

        [HttpGet]
        public IActionResult GetDeptUsersList()
        {
            var user = HttpContext.Session.GetString("username");

            var staff = db.EmEmpData.Where(c => c.EmpNo == user).FirstOrDefault();
            var dept = db.EsDept.Where(c => c.FullCode == staff.Dept).FirstOrDefault();
            if (dept.PreManager == user)
            {
                var lst = db.EmEmpData.Where(c => c.Dept == staff.Dept&&c.LzDate==null ).Select(c=>new { c.Fname,c.EmpNo,c.EBm}).ToList();

                return Ok( new { 
                    code=0,
                    msg="",
                    count=lst.Count,
                    data=lst,


                     }); 
            }
            else {

                return Ok(new { msg="nook"});
            }
        }

        [HttpGet]
        public IActionResult BacthAddOverTime()
        {

            var user = HttpContext.Session.GetString("username");
            if (user != null && user != string.Empty && user != "")
            {
                var staff = db.EmEmpData.Where(c => c.EmpNo == user).FirstOrDefault();
                var dept = db.EsDept.Where(c => c.FullCode == staff.Dept).FirstOrDefault();
                if (dept.ExtPhone == user)
                {
                  //  var lst = db.EmEmpData.Where(c => c.Dept == staff.Dept&&c.LzDate==null).ToList();

                    return View();
                }
                else {

                    return View("other");
                }

               
            }
            else
            {

                return View("other");

            }


        }
    }
}